package com.easemob.chat;

import android.content.ContentValues;
import android.text.TextUtils;
import com.easemob.EMCallBack;
import com.easemob.chat.core.e;
import com.easemob.chat.core.f;
import com.easemob.chat.core.f.c;
import com.easemob.cloud.CloudOperationCallback;
import com.easemob.cloud.HttpFileManager;
import com.easemob.util.EMLog;
import com.easemob.util.PathUtil;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

class ReceiveMessageThread
  implements Runnable
{
  private static final String TAG = "receiver";
  private EMMessage msg;
  private FileMessageBody msgbody;
  private boolean encrypted = false;

  public ReceiveMessageThread(EMMessage paramEMMessage)
  {
    this.msg = paramEMMessage;
    this.msgbody = ((FileMessageBody)paramEMMessage.body);
  }

  public ReceiveMessageThread(EMMessage paramEMMessage, boolean paramBoolean)
  {
    this.msg = paramEMMessage;
    this.msgbody = ((FileMessageBody)paramEMMessage.body);
    this.encrypted = paramBoolean;
  }

  public void run()
  {
    this.msg.status = EMMessage.Status.INPROGRESS;
    String str1 = this.msgbody.localUrl;
    String str2 = this.msgbody.remoteUrl;
    String str3 = this.msgbody.fileName;
    if (this.msg.type == EMMessage.Type.IMAGE)
    {
      if (!TextUtils.isEmpty(((ImageMessageBody)this.msgbody).thumbnailUrl))
        str2 = ((ImageMessageBody)this.msgbody).thumbnailUrl;
    }
    else if ((this.msg.type != EMMessage.Type.VOICE) && (this.msg.type == EMMessage.Type.VIDEO))
      str2 = ((VideoMessageBody)this.msgbody).thumbnailUrl;
    final String str4;
    if (this.msg.type == EMMessage.Type.IMAGE)
    {
      str3 = "th" + str2.substring(str2.lastIndexOf("/") + 1, str2.length());
      str4 = PathUtil.getInstance().getImagePath() + "/" + str3;
    }
    else if (this.msg.type == EMMessage.Type.VIDEO)
    {
      str3 = str2.substring(str2.lastIndexOf("/") + 1, str2.length());
      str4 = PathUtil.getInstance().getImagePath() + "/" + str3;
      ((VideoMessageBody)this.msgbody).localThumb = str4;
      ((VideoMessageBody)this.msgbody).localUrl = (PathUtil.getInstance().getVideoPath() + "/" + str3 + ".mp4");
    }
    else if (this.msg.type == EMMessage.Type.VOICE)
    {
      str3 = str2.substring(str2.lastIndexOf("/") + 1, str2.length());
      if (!EMChatManager.getInstance().getChatOptions().getAudioFileWithExt())
        str4 = PathUtil.getInstance().getVoicePath() + "/" + str3;
      else
        str4 = PathUtil.getInstance().getVoicePath() + "/" + str3 + ".amr";
      this.msgbody.localUrl = str4;
    }
    else if (this.msg.type == EMMessage.Type.FILE)
    {
      str4 = PathUtil.getInstance().getFilePath() + "/" + str3;
      this.msgbody.localUrl = str4;
    }
    else
    {
      str4 = str1;
    }
    if ((TextUtils.isEmpty(str3)) || (str3.equals("th")))
    {
      this.msg.status = EMMessage.Status.FAIL;
      updateMsgState();
      if (this.msgbody.downloadCallback != null)
        this.msgbody.downloadCallback.onError(-1, "file name is null or empty");
      return;
    }
    final String str5 = str4;
    HttpFileManager localHttpFileManager = new HttpFileManager(EMChat.getInstance().getAppContext(), f.a().c().a);
    EMLog.d("receiver", "localUrl:" + this.msgbody.localUrl + " remoteurl:" + str2 + " localThumb:" + str5);
    HashMap localHashMap = new HashMap();
    String str6;
    if (this.msg.type == EMMessage.Type.IMAGE)
    {
      str6 = ((ImageMessageBody)this.msgbody).thumbnailSecret;
      if (TextUtils.isEmpty(str6))
        str6 = this.msgbody.secret;
      if (!TextUtils.isEmpty(str6))
        localHashMap.put("share-secret", str6);
    }
    else if (this.msg.type == EMMessage.Type.VIDEO)
    {
      str6 = ((VideoMessageBody)this.msgbody).thumbnailSecret;
      if (!TextUtils.isEmpty(str6))
        localHashMap.put("share-secret", str6);
    }
    else if ((this.msg.type == EMMessage.Type.VOICE) && (this.msgbody.secret != null))
    {
      localHashMap.put("share-secret", this.msgbody.secret);
    }
    if ((this.msg.type == EMMessage.Type.IMAGE) || (this.msg.type == EMMessage.Type.VIDEO))
      localHashMap.put("thumbnail", "true");
    localHttpFileManager.downloadFile(str2, str5, localHashMap, new CloudOperationCallback()
    {
      public void onSuccess(String paramAnonymousString)
      {
        File localFile = new File(str5);
        EMLog.d("receiver", "file downloaded:" + str5 + " size:" + localFile.length());
        if (ReceiveMessageThread.this.encrypted)
          EMEncryptUtils.decryptFile(localFile.getAbsolutePath(), ReceiveMessageThread.this.msg.getFrom());
        ReceiveMessageThread.this.msgbody.downloaded = true;
        ReceiveMessageThread.this.msg.status = EMMessage.Status.SUCCESS;
        ReceiveMessageThread.this.updateMsgState();
        ReceiveMessageThread.this.msg.progress = 100;
        if (ReceiveMessageThread.this.msgbody.downloadCallback != null)
        {
          ReceiveMessageThread.this.msgbody.downloadCallback.onProgress(100, null);
          ReceiveMessageThread.this.msgbody.downloadCallback.onSuccess();
        }
        if ((ReceiveMessageThread.this.msg.type == EMMessage.Type.VOICE) || (ReceiveMessageThread.this.msg.type == EMMessage.Type.VIDEO))
          ReceiveMessageThread.this.updateMsgBody(ReceiveMessageThread.this.msg);
      }

      public void onError(String paramAnonymousString)
      {
        ReceiveMessageThread.this.msg.status = EMMessage.Status.FAIL;
        EMLog.e("receiver", "download file localThumbnailFilePath:" + str5 + ",error:" + paramAnonymousString);
        if ((str4 != null) && (new File(str4).exists()))
        {
          File localFile = new File(str4);
          try
          {
            if (localFile.isFile())
              localFile.delete();
          }
          catch (Exception localException)
          {
            EMLog.d("receiver", "temp file del fail." + str4);
          }
        }
        ReceiveMessageThread.this.updateMsgState();
        if (ReceiveMessageThread.this.msgbody.downloadCallback != null)
          ReceiveMessageThread.this.msgbody.downloadCallback.onError(-1, paramAnonymousString);
      }

      public void onProgress(int paramAnonymousInt)
      {
        ReceiveMessageThread.this.msg.progress = paramAnonymousInt;
        if (ReceiveMessageThread.this.msgbody.downloadCallback != null)
          ReceiveMessageThread.this.msgbody.downloadCallback.onProgress(paramAnonymousInt, null);
      }
    });
  }

  protected void updateMsgState()
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("status", Integer.valueOf(this.msg.status.ordinal()));
    e.a().a(this.msg.getMsgId(), localContentValues);
  }

  protected void updateMsgBody(EMMessage paramEMMessage)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("msgbody", MessageEncoder.getJSONMsg(paramEMMessage, true));
    e.a().a(this.msg.getMsgId(), localContentValues);
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.ReceiveMessageThread
 * JD-Core Version:    0.6.1
 */